/*	
	
	Copyright (c) 2007-2009 Ryan Christiansen
	
	This software is provided 'as-is', without any express or implied
	warranty. In no event will the authors be held liable for any damages
	arising from the use of this software.
	
	Permission is granted to anyone to use this software for any purpose,
	including commercial applications, and to alter it and redistribute it
	freely, subject to the following restrictions:
	
	1. The origin of this software must not be misrepresented; you must not
	claim that you wrote the original software. If you use this software
	in a product, an acknowledgment in the product documentation would be
	appreciated but is not required.
	
	2. Altered source versions must be plainly marked as such, and must not be
	misrepresented as being the original software.
	
	3. This notice may not be removed or altered from any source
	distribution.
	
*/
package funit.framework.writers
{
	import funit.framework.MessageString;
	import funit.framework.MessageUtils;
	import funit.framework.comparers.AssertEqualityComparer;
	
	import mx.collections.IList;
	import mx.utils.StringUtil;
	
	import sv.utils.ClassUtil;
	
	public class EqualMessageWriter extends MessageWriter
	{
		
		
		public function EqualMessageWriter(message:String=null, messageParameters:Array=null)
		{
			super(message, messageParameters);
		}
		
		
		/**
		 * @param expected
		 * @param actual
		 */
		override public function displayDifferences( expected:Object, actual:Object ) : void
		{
			var mismatch:int;
			
			if (expected is String && actual is String)
			{
				// find collection mismatch position
				mismatch = MessageUtils.findMismatchPosition(String(expected), String(actual), 0, false);
				
				displayStringDifferences(String(expected), String(actual), mismatch);
			}
			else if ( (expected is Array && actual is Array) ||
					  (expected is IList && actual is IList) )
			{
				// find collection mismatch position
				var comparer:AssertEqualityComparer = new AssertEqualityComparer();
				comparer.equals(expected, actual);
				mismatch = int(comparer.collectionFailures[0]);
				
				displayCollectionDifferences(expected, actual, mismatch);
			}
			else
			{
				super.displayDifferences(expected, actual);
			}
		}
		
		/**
		 * @param expected
		 * @param actual
		 * @param mismatch
		 * @param ignoreCase
		 * @param clipping
		 */
		override public function displayStringDifferences( expected:String, actual:String, mismatch:int, ignoreCase:Boolean = false, clipping:Boolean = true ) : void
		{
			if (expected.length == actual.length)
			{
				writeMessageLine(
					0,
					"String lengths are both {0}. Strings differ at index {1}.",
					expected.length,
					mismatch
				);
			}
			else
			{
				writeMessageLine(
					0,
					"Expected string length {0} but was {1}. Strings differ at index {2}.",
					expected.length,
					actual.length,
					mismatch
				);
			}
			
			super.displayStringDifferences(expected, actual, mismatch, ignoreCase, clipping);
		}
		
		/**
		 * @param expected
		 * @param actual
		 * @param mismatch
		 */
		public function displayCollectionDifferences( expected:Object, actual:Object, mismatch:int ) : void
		{
			var expectedLength:int = getLength(expected);
			var actualLength:int = getLength(actual);
			
			displayCollectionTypesAndSizes(expected, actual);
			
			writeMessageLine(0, "Values differ at index [{0}]", mismatch);
			
			if (expectedLength == actualLength)
			{
				displayDifferences(
					expected[mismatch],
					actual[mismatch]
				);
			}
			else if (expectedLength < actualLength)
			{
				write( "  Extra:    " );
				writeCollectionElements( actual, mismatch, 3 );
				writeLine();
			}
			else
			{
				write( "  Missing:  " );
				writeCollectionElements( expected, mismatch, 3 );
				writeLine();
			}
		}
		
		/**
		 * @param expected
		 * @param actual
		 */
		private function displayCollectionTypesAndSizes( expected:Object, actual:Object ) : void
		{
			var expectedType:Class = ClassUtil.getClass(expected);
			var actualType:Class = ClassUtil.getClass(actual);
			
			var expectedMessage:String = StringUtil.substitute(MessageString.DefaultFormat, ClassUtil.getQualifiedClassName(expectedType));
			expectedMessage += StringUtil.substitute(" with {0} elements", getLength(expected));
			
			var actualMessage:String = StringUtil.substitute(MessageString.DefaultFormat, ClassUtil.getQualifiedClassName(actualType));
			actualMessage += StringUtil.substitute(" with {0} elements", getLength(actual));
			
			if (expectedMessage == actualMessage)
			{
				writeMessageLine(0, "Expected and actual are both {0}", expectedMessage);
			}
			else
			{
				writeMessageLine(0, "Expected is {0}, actual is {1}", expectedMessage, actualMessage);
			}
		}
		
	}
	
}